package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.adapter.HotShowVarietyAdapter;
import flc.ast.adapter.LatestRecAdapter;
import flc.ast.adapter.NewAnimationAdapter;
import flc.ast.databinding.FragmentHotBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HotFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HotFragment extends BaseNoModelFragment<FragmentHotBinding> {

    private LatestRecAdapter recAdapter;
    private HotShowVarietyAdapter varietyAdapter;
    private NewAnimationAdapter animationAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_hot;
    }

    @Override
    protected void initView() {
        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recAdapter = new LatestRecAdapter();
        mDataBinding.rvRecommendList.setAdapter(recAdapter);
        recAdapter.setOnItemClickListener(this);

        mDataBinding.rvVarietyList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        varietyAdapter = new HotShowVarietyAdapter();
        mDataBinding.rvVarietyList.setAdapter(varietyAdapter);
        varietyAdapter.setOnItemClickListener(this);

        mDataBinding.rvAnimationList.setLayoutManager(new LinearLayoutManager(mContext));
        animationAdapter = new NewAnimationAdapter();
        mDataBinding.rvAnimationList.setAdapter(animationAdapter);
        animationAdapter.setOnItemClickListener(this);

        mDataBinding.refreshAnimationItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshAnimationItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshAnimationItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getNewAnimationData();
                mDataBinding.refreshAnimationItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getNewAnimationData();
                mDataBinding.refreshAnimationItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        getLatestRecommendData();
//        getHotShowVarietyData();
        getNewAnimationData();
    }

    private void getLatestRecommendData() {
        StkResApi.getTagResourceList(this, AppConfig.LATEST_RECOMMEND_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getHotShowVarietyData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_SHOW_VARIETY_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        varietyAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getNewAnimationData() {
        StkResApi.getTagResourceList(this, AppConfig.NEW_ANIMATION_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            animationAdapter.setList(result);
                        } else {
                            animationAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

}